package com.hulushuju.core.net.http.callback;

import com.hulushuju.core.net.ContentType;
import com.hulushuju.core.net.exception.HttpException;
import com.hulushuju.core.net.exception.UnSupportedTypeException;
import com.hulushuju.core.net.http.HttpRequest;
import com.hulushuju.core.net.http.HttpResponse;
import com.hulushuju.core.util.DateManager;
import com.hulushuju.core.util.FileManager;

import java.util.Date;
import java.util.Objects;

/**
 * @Author: Hanjiafeng
 * @Date: 2019-05-30
 * @Desc: 回调实现
 */
public class HttpCallbackImpl implements HttpCallback {
    @Override
    public void success(HttpRequest httpRequest, HttpResponse httpResponse) {
        StringBuilder contents = new StringBuilder();
        try {
            //读取内容类型
            ContentType contentType = httpResponse.getContentType();
            //比较父类型,只记录文本内容
            if (!ContentType.TEXT.equalsSuperType(contentType)) {
                throw new UnSupportedTypeException("记录请求只支持文本类型");
            }

            //记录发送请求和接受响应时间
            long sendTime = httpResponse.getSendRequestMillis();
            long receiveTime = httpResponse.getReceivedResponseMillis();
            contents.append("Consume Time:").append(receiveTime - sendTime).append("ms").append("\n");

            //请求参数
            StringBuilder tempSB = record(httpRequest);
            contents.append(tempSB);

            //响应参数,获取返回文本
            String body = httpResponse.getContent(String.class);
            contents.append("Response Body:").append("\n").append(body).append("\n");

            //写入本地
            FileManager.writeNew("success", contents.toString(), contentType.getSubType());
        } catch (HttpException e) {
            //todo 写入日志
            e.printStackTrace();
        }
    }

    @Override
    public void failure(HttpRequest httpRequest, Exception exception) {
        StringBuilder contents = record(httpRequest);
        contents.append("Exception:\n").append(exception);

    }

    /**
     * 记录HttpRequest中的信息
     *
     * @param httpRequest 请求对象
     * @return StringBuilder
     */
    private StringBuilder record(HttpRequest httpRequest) {
        StringBuilder contents = new StringBuilder();
        //请求参数
        String startTime = DateManager.formatDate(new Date(httpRequest.getBuiltTime()), null);
        contents.append("Start Time:").append(startTime).append("ms").append("\n");
        String url = httpRequest.getUrl();
        contents.append("URL:").append(url).append("\n");
        //请求头
        String headers = httpRequest.getHeaders().toString();
        contents.append("Request Headers:").append(headers).append("\n");
        //请求方法
        String method = httpRequest.getMethod().getMethod();
        contents.append("Request Method:").append(method).append("\n");
        //请求体参数
        String formData = httpRequest.getFormData();
        //如果不为null,记录请求参数
        if (!Objects.isNull(formData)) {
            contents.append("Request Form Data:").append(formData).append("\n");
        }

        return contents;
    }
}
